﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class MapCube : MonoBehaviour {

    [HideInInspector]
    public GameObject TurretGo;//保存当前cube身上的炮台
    [HideInInspector]
    public TurretData turretData;
    public GameObject buildEffect;
    private Renderer Renderer;
    [HideInInspector]
    public  bool isUpgrade = false;

    void Start()
    {
        Renderer = GetComponent<Renderer>();
    
    }

    public void BuildTurret(TurretData  turretData)
    {
        this.turretData = turretData;
        isUpgrade = false;
        TurretGo = GameObject.Instantiate(turretData.TurretPrefab, transform.position, Quaternion.identity);
        GameObject effect = GameObject.Instantiate(buildEffect, transform.position, Quaternion.identity);
        Destroy(effect, 1.5f);
    }
    public void UpgradeTurret()
    {
        if (isUpgrade == true) return;
        Destroy(TurretGo);
        isUpgrade = true;
        TurretGo = GameObject.Instantiate(turretData.turretUpgradePrefab, transform.position, Quaternion.identity);
        GameObject effect = GameObject.Instantiate(buildEffect, transform.position, Quaternion.identity);
        Destroy(effect, 1.5f);
    
    }
    public void DestroyTurret()
    {
        Destroy(TurretGo);
        isUpgrade = false;
        turretData = null;
        TurretGo = null;
        GameObject effect = GameObject.Instantiate(buildEffect, transform.position, Quaternion.identity);
        Destroy(effect, 1.5f);
    
    }
    void OnMouseEnter()
    {
        if (TurretGo == null && EventSystem.current.IsPointerOverGameObject() == false )
        {
            Renderer.material.color = Color.red;
        
        }
    
    }
    void OnMouseExit()
    {
        Renderer.material.color = Color.white;
    }
}
